package com.lxr.test5002;

import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.Button;
import android.widget.TextView;

import com.lxr.test5002.base.BaseActivity;

import com.lxr.test5002.utils.MyUtil;
import com.white.progressview.HorizontalProgressView;

import java.io.IOException;
import java.text.SimpleDateFormat;

import butterknife.BindView;

public class AudioActivity extends BaseActivity {
    @BindView(R.id.tv_audio_title)
    TextView mTvTitle;
    @BindView(R.id.tv_phone_tip)
    TextView mTvTip;
    @BindView(R.id.tv_phone_test_time)
    TextView mTvTime;
    @BindView(R.id.pb_phone_test)
    HorizontalProgressView mProgress;
    @BindView(R.id.btn_phone_start)
    Button mBtnStart;
    private MediaPlayer mMediaPlayer;
    private AssetManager mAssetManager;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
        }
    };
    private long mMusicTime, mMaxTime;

    private Runnable musicRunnable = new Runnable() {
        @Override
        public void run() {
            mProgress.setProgress((int) mMusicTime);
            mTvTime.setText(formatTime(mMusicTime));
            mMusicTime += 100;
            handler.postDelayed(this, 100);
        }
    };

    public AudioActivity() {
        super(R.layout.activity_phone);
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
    }
    @Override
    protected void initData() {
        mTvTitle.setText("音频");
        mAssetManager = getAssets();
    }

    @Override
    protected void initEvent() {
        mBtnStart.setOnClickListener(v -> {
            mBtnStart.setEnabled(false);
            prepareMusic();
        });
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        realseMusic();
    }

    private void prepareMusic() {
        MyUtil.setAudioManager();
        if (mMediaPlayer == null) {
            mMediaPlayer = new MediaPlayer();
        } else {
            mMediaPlayer.reset();
        }
        try {
            AssetFileDescriptor fileDescriptor = mAssetManager.openFd("phone.wav");
            mMediaPlayer.setDataSource(fileDescriptor.getFileDescriptor(),
                    fileDescriptor.getStartOffset(),
                    fileDescriptor.getLength());
            mMediaPlayer.prepare();
            mMusicTime = 0;
            mMaxTime = mMediaPlayer.getDuration();
            mProgress.setMax((int) mMaxTime);
            mMediaPlayer.setOnCompletionListener(mp -> {
                mp.stop();
                handler.removeCallbacks(musicRunnable);
                mProgress.setProgress(0);
                mTvTime.setText(formatTime(0));
                mTvTip.setText("测试完毕");
                mBtnStart.setEnabled(true);
            });
            startMusic();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void startMusic() {
        mTvTip.setText("正在进行喇叭测试");
        mTvTime.setText("播放已开始");
        if (!mMediaPlayer.isPlaying()) {
            handler.post(musicRunnable);
            mMediaPlayer.start();
        }
    }


    private void realseMusic() {
        if (mMediaPlayer != null) {
            mMediaPlayer.reset();
            mMediaPlayer.release();
        }
    }

    private String formatTime(long time) {
        SimpleDateFormat formatter = new SimpleDateFormat("mm:ss");
        String formatTime = formatter.format(time);
        return formatTime;
    }
}